<template>
  <div>
    <q-layout>
      <q-layout-header ref="header">
        <xheader :title="title"></xheader>
      </q-layout-header>
      <q-page-container>
        <xscrollarea :scroll-height="scrollHeight">
          <q-page padding>
            <q-card v-for="(d,index) in data">
              <q-card-title>
                #{{index+1}} -{{d.name}}-{{d.code}}
              </q-card-title>
              <q-card-separator/>
              <q-card-main>
                <div class="row" v-for="dd in d.list">
                  <div class="col"><p>{{dd.label}}：</p></div>
                  <div class="col"><p>{{dd.value}}</p></div>
                </div>
              </q-card-main>
              <q-card-separator/>
              <q-card-actions>

                <q-btn v-if="d.JLSTATUS === '待审核'" label="审核" class="full-width" color="primary"
                       @click="popDeal(index)"/>
                <q-btn v-else label="已审核" class="full-width" color="primary" disable/>
              </q-card-actions>
            </q-card>
          </q-page>
        </xscrollarea>
      </q-page-container>
    </q-layout>
    <q-modal v-model="showDealPopup" minimized>
      <q-modal-layout>
        <q-toolbar slot="header">

          <q-toolbar-title>
            设备备品配件领料单
          </q-toolbar-title>
          <q-btn
            flat
            round
            dense
            @click="showDealPopup = false"
            icon="clear"
          />
        </q-toolbar>

        <div class="layout-padding" v-if="dealIndex !== undefined">
          <q-card>
            <q-card-title>
              {{data[dealIndex].name}}-{{data[dealIndex].code}}
            </q-card-title>
            <q-card-separator/>
            <q-card-main>
              <q-input
                v-model="kcNum"
                float-label="库存数量"
                readonly
              />
              <q-input float-label="领用数量" v-model="num" type="number" clearable/>
              <br>
              <q-radio v-model="option" val="passed" label="通过"/>
              <q-radio v-model="option" val="Notthrough" label="不通过"/>
            </q-card-main>
            <q-card-separator/>
          </q-card>
        </div>

        <q-toolbar slot="footer">
          <q-btn label="确认" class="full-width" color="primary" @click="submit()"/>
        </q-toolbar>
      </q-modal-layout>
    </q-modal>
  </div>
</template>

<script>
  import { spare_parts_record_map } from '../../js/table-map'
  import Xheader from '../../components/xheader'
  import Xfilterheader from '../../components/xfilterheader'
  import Xscrollarea from '../../components/xscrollarea'

  export default {
    name: 'spare_parts_record',
    components: { Xscrollarea, Xfilterheader, Xheader },
    props: {
      title: {}
    },
    mounted () {
      this.scrollHeight = this.$parent.webHeight - this.$refs.header.$el.clientHeight
    },
    data: () => ({
      data: [],
      dealIndex: undefined,
      kcNum: '',
      num: '',
      option: 'passed',
      showDealPopup: false,
      scrollHeight: 0
    }),
    methods: {
      getData () {
        this.axios.post(this.$conf.server + '/SparepartsRecord/InquireallRecord').then(res => {
          let dd = res.data.Stocks
          if (dd) {
            for (let d of dd) {
              let data = {}
              let list = []
              for (let a in d) {
                let value = d[a]
                if (a === 'WLCODE') {
                  data.code = value
                }
                if (a === 'WLNAME') {
                  data.name = value
                } else {
                  let key = spare_parts_record_map.colMap.get(a)
                  if (key && key !== '') {
                    list.push({ 'label': key, 'value': value })
                  }
                }
              }
              data.list = list
              Object.assign(data, d)
              this.data.push(data)
            }
          }
        })
      },
      popDeal (index) {
        this.dealIndex = index
        this.showDealPopup = true
        this.axios.post(this.$conf.server + '/SparepartsRecord/SumMaterielsresult', this.$qs.stringify({
          toolCode: this.data[index].code
        })).then(res => {
          if (res.data.result) {
            this.kcNum = res.data.sumnumbers[0].QTY;
            this.num = this.data[index].LYNUMBERS
          } else {
            this.$parent.error("处理失败");
          }
        })

      },
      submit () {
        this.$parent.confirm('提示', `确定？`, () => {
          this.axios.post(this.$conf.server + '/SparepartsRecord/SparepartsRecordexamine', this.$qs.stringify({
            KCNUMBERS: this.kcNum,
            CKNUMBERS: this.num,
            YCCKNUMBERS: this.data[this.dealIndex].LYNUMBERS,
            MATERIEL: this.data[this.dealIndex].WLID,
            WLLYJL: this.data[this.dealIndex].JLID,
            RESULTS: this.option
          })).then(res => {
            this.$parent.info("领用申请已审核！");
            this.kcNum = ''
            this.num = ''
            this.showDealPopup = false
            this.getData()
          })
        })
      }
    },
    created () {
      this.getData()
    }
  }
</script>

<style scoped>

</style>
